package com.tmall.tmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmall.tmall.mapper.OrderitemMapper;
import com.tmall.tmall.mapper.ProductOrderEvaluateMapper;
import com.tmall.tmall.pojo.Comment;
import com.tmall.tmall.pojo.CommentRt;
import com.tmall.tmall.pojo.PageRt;
import com.tmall.tmall.pojo.ProductRt;
import com.tmall.tmall.service.ProductOrderEvaluateService;
import com.tmall.tmall.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductOrderEvaluateServiceImpl implements ProductOrderEvaluateService {
    @Autowired
    private ProductOrderEvaluateMapper productOrderEvaluateMapper;
    @Autowired
    private OrderitemMapper orderitemMapper;

    @Override
    @Transactional
    public void save(Comment comment) {
        comment.setUid(ThreadLocalUtils.getUid());
        comment.setCreateDate(LocalDateTime.now());
        //纯如评论
        productOrderEvaluateMapper.save(comment);
        //靠pid和oid修改订单为已完成
        orderitemMapper.ok(comment.getProductOrderId(),comment.getProductId(),5);
    }

    @Override
    public PageRt page(Integer pageNum, Integer pageSize, String username, String productName, String content, Integer productId) {
        PageHelper.startPage(pageNum, pageSize);
        List<CommentRt> list = productOrderEvaluateMapper.page(username,productName,productId,content);
        PageInfo<CommentRt> pageInfo = new PageInfo<>(list);
        return new PageRt(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void update(Comment comment) {
        productOrderEvaluateMapper.update(comment);
    }

    @Override
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
            Comment comment = Comment.builder().id(id).deleteAt(LocalDateTime.now()).build();
            productOrderEvaluateMapper.update(comment);
        }
    }
}
